package com.spareparts.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.spareparts.R;
import com.spareparts.activity.base.BaseActivity;

/**
 * 
 * @author WANG
 *
 */
@SuppressLint("HandlerLeak")
public class SparePartsSearchActivity extends BaseActivity
{
	private Spinner mSpinnerFactory;
	private ListView mListView;
	private FrameLayout mFrameLayout01;
	
	private View mOperationView;
	
	private Context mContext;
	
	private ImageView mOperationDetail;
	private ImageView mOperationUpdate;
	private ImageView mOperationDelete;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_spare_search);
		mContext = this;
		
		mSpinnerFactory = (Spinner) findViewById(R.id.spinnerFactory);
		mListView = (ListView) findViewById(R.id.listView);
		mFrameLayout01 = (FrameLayout) findViewById(R.id.frameLayout01);
		
		//设置Operation
		initOperationData();
		
		//设置下拉菜单数据
		ArrayAdapter<Object> factoryAdapter = new ArrayAdapter<Object>(this, 
				android.R.layout.simple_spinner_item, new String[]{"三星", "我勒个去的的额额嗯嗯额额额嗯嗯"}); 
		factoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
		mSpinnerFactory.setAdapter(factoryAdapter);
		
		//设置ListView
		ListAdapter la = new ListAdapter();
		mListView.setAdapter(la);
		
		mFrameLayout01.addView(mOperationView);
	}
	
	/**
	 * 设置Operation
	 */
	private void initOperationData()
	{
		mOperationView = LayoutInflater.from(mContext).inflate( R.layout.view_operation, null);
		mOperationDetail = (ImageView) mOperationView.findViewById(R.id.ImageViewDescription);
		mOperationUpdate = (ImageView) mOperationView.findViewById(R.id.ImageViewUpdate);
		mOperationDelete = (ImageView) mOperationView.findViewById(R.id.ImageViewDelete);
		
		//详情事件
		mOperationDetail.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				showToast("你点击了详情");
			}
		});
		//详情事件
		mOperationUpdate.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				showToast("你点击了修改");
			}
		});
		mOperationUpdate.setVisibility(8);
		//详情事件
		mOperationDelete.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) 
			{
				showToast("你点击了删除");
			}
		});
		mOperationDelete.setVisibility(8);
	}
	
	/**
	 * handler刷新UI
	 */
	private Handler mHandler = new Handler() 
	{
		@Override
		public void dispatchMessage(Message msg) 
		{
			super.dispatchMessage(msg);
			
			switch(msg.what)
			{
				case 1:
					//弹出并设置OperationView位置
					mOperationView.layout(msg.arg1, msg.arg2,
							msg.arg1+50, msg.arg2+50);
				default:
					break;
			}
			
		}
	};
	
	
	/**
	 * 设置listView Adapter
	 * @author WANG
	 *
	 */
	private class ListAdapter extends BaseAdapter
	{

		@Override
		public int getCount() 
		{
			return 6;
		}

		@Override
		public Object getItem(int arg0) 
		{
			return null;
		}

		@Override
		public long getItemId(int position) 
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			if ( convertView == null )
			{
				convertView=LayoutInflater.from(mContext).inflate( R.layout.listview_spare_search, null);
			}
			
			//设置操作按钮的图片单击事件
			final TextView tv = (TextView) convertView.findViewById(R.id.textViewOperation);
			tv.setOnClickListener(new OnClickListener() 
			{
				@Override
				public void onClick(View v) 
				{
					final int[] location = new int[2];
					tv.getLocationOnScreen(location);
					System.out.println("x="+location[0]+",y="+(location[1]-mListView.getTop()));
					Message msg = new Message();
					msg.what = 1;
					msg.arg1 = location[0];
					msg.arg2 = location[1]-mListView.getTop();
					mHandler.sendMessage(msg);
				}
			});
			
			return convertView;
		}
		
	}

}
